package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

/**
 * adb connect <手机IP地址>:<端口号>
 * adb devices
 */

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private MyBroadcastReceiver myBroadcastReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myBroadcastReceiver = new MyBroadcastReceiver();
// 创建IntentFilter对象，并添加接收的广播动作
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.example.MY_CUSTOM_ACTION");
// 注册广播接收器
        registerReceiver(myBroadcastReceiver, filter);


        findViewById(R.id.btn_JobIntentService).setOnClickListener(this);
        findViewById(R.id.btn_BroadcastReceiver).setOnClickListener(this);
        findViewById(R.id.btn_images).setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myBroadcastReceiver);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
//        setIntent();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_JobIntentService:
                long id = Thread.currentThread().getId();
                System.out.println("xd===Thread_ID=======" + id);

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    int JOB_ID = 8;
                    Intent work = new Intent();
                    work.putExtra("key", "value555555");
                    MyJobIntentService.enqueueWork(MainActivity.this, MyJobIntentService.class, JOB_ID, work);
                }
                break;
            case R.id.btn_BroadcastReceiver:
                // 创建Intent对象
                Intent intent = new Intent();
                // 设置广播的动作
                intent.setAction("com.example.MY_CUSTOM_ACTION");
                // 发送有序广播
                sendOrderedBroadcast(intent, null);
                break;

            case R.id.btn_images:
                startActivity(new Intent(this, ImagesActivity.class));
                break;
        }
    }
}
